<template>
  <div style="height: 100%">
    <el-container style="height: 100%">
      <el-aside :width="menuCollapse ? '64px' : '200px'">
<!--        菜单栏-->
          <el-menu
            router
            :default-active="activeMenu"
            :collapse="menuCollapse"
            class="el-menu-vertical-demo"
            background-color="#304156"
            text-color="#bfcbd9"
            active-text-color="#409eff">
            <el-menu-item index="/welcome">首页</el-menu-item>
            <!--一级子菜单-->
            <el-submenu :index="formatPath(null, item.name)" v-for="item in firstMenus" :key="item.id">
            <!--一级子菜单标题-->
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{item.title}}</span>
              </template>
                <!--二级子菜单-->
                <el-menu-item class="menu-second" :index="formatPath(item.name, subItem.name)" :key="subItem.id" v-for="subItem in secondMenus(item.id)">{{subItem.title}}</el-menu-item>
              </el-submenu>
          </el-menu>
      </el-aside>
      <el-container>
        <el-header>
          <el-button :icon="menuCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold' " @click="menuCollapse = !menuCollapse"></el-button>
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              <el-avatar src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"></el-avatar>
                欢迎您 {{loginUser.username}}
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="change">修改密码</el-dropdown-item>
              <el-dropdown-item command="quit">退出</el-dropdown-item>

            </el-dropdown-menu>
          </el-dropdown>
        </el-header>
<!--        点击菜单的时候，不是全部页面跳转，只是这main的部分跳转，也就是组件变化 路由解析-->
        <router-view />
      </el-container>
    </el-container>
  </div>

</template>

<script>
import { api_logout, api_info } from '../api/admin.js';
export default {
  data() {
    return {
      // 默认是展开的
      menuCollapse: false,
      // 点击菜单，字体颜色
      activeMenu: '',
      // 拿到登录用户的信息，绑定到这里
      loginUser: {
        username: '',
        icon: ''
      },
      menus: []
    };
  },
  computed: {
    firstMenus() {
      return this.menus.filter(item => item.parentId === 0);
    },
    secondMenus() {
      return parendId => this.menus.filter(item => item.parentId === parendId);
    }
  },
  mounted() {
    // 这里就是每次都去session中拿
    this.activeMenu = window.sessionStorage.getItem('activeMenu');
    if (!this.activeMenu) {
      this.activeMenu = '/welcome';
    }
    // 在这里调用getInfo
    this.getInfo();
  },
  watch: {
    $route(to) {
      // console.log(to);
      this.selectMenu(to.path);
    }
  },
  methods: {
    async getInfo() {
      const { data: res } = await api_info();
      console.log(res);
      if (res) {
        this.loginUser.icon = res.data.icon;
        this.loginUser.username = res.data.username;
        this.menus = res.data.menus;
      }
    },
    selectMenu(index) {
      // 要去的页面
      console.log(index);
      // 即将离开的页面
      // console.log(indexPath);
      this.activeMenu = index;
      // 每次点击之后就将他保存到session之中
      window.sessionStorage.setItem('activeMenu', index);
    },
    handleCommand(command) {
      console.log(command);
      if (command === 'quit') {
        this.handleLogout();
      }
    },
    async handleLogout() {
      const { data: res } = await api_logout();
      console.log(res);
      // 退出失败
      if (res.code !== 200) {
        this.$message.error(res.message);
        return;
      }
      // 清除session
      window.sessionStorage.clear();
      // 跳转到登录页面
      this.$router.replace('/login');
    },
    formatPath(folder, name) {
      name = '/' + name;
      if (folder) {
        name = '/' + folder + name;
      }
      return name;
    }
  }
};
</script>

<style>
  .el-header, .el-footer {
    background-color: #ffffff;
    text-align: center;
    line-height: 60px;
    /*flex弹性布局*/
    display: flex;
    /* 内容向两边分散 */
    justify-content: space-between;
    align-items: center;
    border-bottom: #D3DCE6 solid 1px;
  }

  .el-avatar {
    position: relative;
    top: 12px;
  }
  .menu-second {
    /*!important 不允许被修改*/
    background-color: #1f2d3d!important;
  }
  .menu-second:hover {
    background-color: #001528!important;
  }

  .el-aside {
    background-color: #304156;
    text-align: center;
    line-height: 200px;
  }

  .el-main {
    background-color: #E9EEF3;
    color: #333;
    text-align: center;
  }

  body > .el-container {
    margin-bottom: 40px;
  }

  .el-container:nth-child(5) .el-aside,
  .el-container:nth-child(6) .el-aside {
    line-height: 260px;
  }

  .el-container:nth-child(7) .el-aside {
    line-height: 320px;
  }
  .el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
  }
  .el-icon-arrow-down {
    font-size: 12px;
  }
  .el-menu {
    border: none
  }
  .el-menu>.el-menu-item, .el-submenu>.el-submenu__title {
    text-align: left;
  }
</style>
